package cn.amebius.simpledocker.controller;

import cn.amebius.simpledocker.controller.request.LoginRequest;
import cn.amebius.simpledocker.controller.request.ResetRequest;
import cn.amebius.simpledocker.service.LoginService;
import cn.amebius.simpledocker.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 阿伟
 * @date 2023/7/27 19:47
 */

@RestController
@Slf4j
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /*登录接口*/
    @PostMapping("/login")
    public Response<String> login(@RequestBody LoginRequest loginRequest) {
        loginRequest.checkParameter();
        String token = this.loginService.login(loginRequest.getUsername(), loginRequest.getPassword());
        return Response.ok(token);
    }

    /*登出接口*/
    @PostMapping("/logout")
    public Response<String> logout() {
        this.loginService.logout();
        return Response.ok("登出成功");
    }

    @PutMapping("/reset")
    public Response<String> reset(@RequestBody ResetRequest request) {
        this.loginService.reset(request.getOldPassword(),request.getNewPassword());
        return Response.ok("修改密码成功！");
    }

}
